package com.xmmj.baselibrary.widget.permission

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.xmmj.baselibrary.BR
import com.xmmj.baselibrary.R
import com.xmmj.baselibrary.base.dialog.BaseMvvmDialog
import com.xmmj.baselibrary.databinding.DialogPermissionBinding

class PermissionDialog : BaseMvvmDialog<DialogPermissionBinding, PermissionViewModel>() {
    private var title: String? = null
    private var content: String? = null
    private var confirmCallback: ((PermissionDialog) -> Unit)? = null
    private var cancelCallback: ((PermissionDialog) -> Unit)? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mDataBinding.tvTitle.text = title
        mDataBinding.tvContent.text = content
        mDataBinding.btnCancel.visibility =
            if (cancelCallback != null) View.VISIBLE else View.GONE
        mDataBinding.btnConfirm.visibility =
            if (confirmCallback != null) View.VISIBLE else View.GONE
        mDataBinding.btnCancel.setOnClickListener {
            cancelCallback?.invoke(this)
        }
        mDataBinding.btnConfirm.setOnClickListener {
            confirmCallback?.invoke(this)
        }
    }

    override fun getVariableId(): Int {
        return BR.viewModel
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_permission
    }

    override fun getWidth(): Float {
        return resources.getDimension(R.dimen.dp270)
    }

    override fun getHeight(): Float {
        return WindowManager.LayoutParams.WRAP_CONTENT.toFloat()
    }

    fun setTitle(title: String?) {
        this.title = title
    }

    fun setContent(content: String?) {
        this.content = content
    }

    fun setOnCancelListener(callback: (PermissionDialog) -> Unit) {
        this.cancelCallback = callback
    }

    fun setOnConfirmListener(callback: (PermissionDialog) -> Unit) {
        this.confirmCallback = callback
    }
}